package com.springboot.hotel.xencryption;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class MD5Util {
    public static String MD5(String password, String saltStr) {
        try {
//        创建MessageDigest 对象制定算法为MD5
            MessageDigest md = MessageDigest.getInstance("MD5");
//        将盐值与输入字符串连接
            byte[] satedInputBytes = password.concat(saltStr).getBytes();
//        使用MessageDigest 更新字节数组
            md.update(satedInputBytes);
//         执行加密操作
            byte[] encryptedBytes = md.digest();
//         将加密后的字节数组与盐值一起进行编码
            String encrytedString = Base64.getEncoder().encodeToString(encryptedBytes);
            return encrytedString;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void main (String[] args){
        System.out.println(MD5("123456", "admin"));
        System.out.println(MD5("123456", "cleaner"));
        System.out.println(MD5("123456", "operator"));
    }
}
